<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Captcha;
use Markdown;
use App\Model\Art;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //查询
         $arts = DB::table('post')->orderBy('id','title')->paginate(4);

         return view('art.index',['arts'=>$arts]);

      /* $phraseBuilder = new PhraseBuilder(5, '0123456789');
    $captcha = new CaptchaBuilder(null, $phraseBuilder );*/
    
    // echo captcha::img();
    // echo captcha::src();

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('art.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $data = $request->except('_token');
        $res = DB::table('post')->insert($data);
        if($res){
            return redirect('/art');
        }
        else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
         $art = Art::find($id);
       return view('art.show',['art'=>$art]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $art = DB::table('post')->where('id','=',$id)->first();
        return view('art.edit',['art'=>$art]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $art = Art::where('id','=',$id)
            ->update($request->except('_token','_method'));

        return redirect('/art');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $res = DB::table('post')->where('id',$id)->delete();
        return redirect('/art');
    }


}
